﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RVEAd.Business;
using RVEAd.Business.Model;
using RVEAd.Helpers;

namespace RVEAd.Forms
{
    public partial class FormDetaliiPlanMonth : Form
    {
        #region fields

        private RveBL rveBL;

        #endregion

        #region properties

        public int ClientAdId { get; set; }
        public string ClientName { get; set; }

        public int SelectedMonth { get; set; }
        public int SelectedYear { get; set; }
        #endregion

        #region ctor

        public FormDetaliiPlanMonth()
        {
            InitializeComponent();

            this.Load += new EventHandler(FormDetaliiPlanMonth_Load);
            this.dataGridMonthView.KeyDown += new KeyEventHandler(dataGridMonthView_KeyDown);
        }
        
        #endregion

        void FormDetaliiPlanMonth_Load(object sender, EventArgs e)
        {
            rveBL = new RveBL();
            List<AdPlanModel> adPlanModel = rveBL.GetAdPlans(SelectedYear, SelectedMonth, ClientAdId);

            dataGridMonthView.AutoGenerateColumns = false;
            dataGridMonthView.DataSource = adPlanModel;

            lblClientAndDate.Text = string.Format("{0} la {1}-{2}", ClientName, GetMonth(SelectedMonth), SelectedYear);
        }
        void dataGridMonthView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > 0)
            {
                var cell = dataGridMonthView.Rows[e.RowIndex].Cells[e.ColumnIndex];
                cell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

                if (cell.Value != null)
                {
                    if (cell.Value.ToString().Equals("true", StringComparison.InvariantCultureIgnoreCase))
                    {
                        cell.Style.BackColor = Color.GreenYellow;
                    }
                    else if (cell.Value.ToString().Equals("false", StringComparison.InvariantCultureIgnoreCase))
                    {
                        cell.Style.BackColor = Color.White;
                    }

                    cell.Value = string.Empty;
                }
            }

            //if (e.RowIndex > -1 && e.ColumnIndex > 0)
            //{
            //    var cell = dataGridMonthView.Rows[e.RowIndex].Cells[e.ColumnIndex];
            //    cell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

            //    if (!string.IsNullOrEmpty(cell.Value.ToString().Trim()))
            //    {
            //        cell.Style.BackColor = Color.GreenYellow;
            //    }
            //    else
            //    {
            //        cell.Style.BackColor = Color.FromArgb(252, 167, 188);
            //    }
            //}
        }
        void dataGridMonthView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.X && dataGridMonthView.CurrentCell != null && dataGridMonthView.CurrentCell.ColumnIndex > 0)
            {
                int rowIndex = dataGridMonthView.CurrentCell.RowIndex;
                int columnIndex = dataGridMonthView.CurrentCell.ColumnIndex;

                if (dataGridMonthView.CurrentCell.Style.BackColor == Color.White || dataGridMonthView.CurrentCell.Style.BackColor.IsEmpty)
                {
                    dataGridMonthView.CurrentCell.Style.BackColor = Color.GreenYellow;
                    SetIntervalValue(columnIndex, rowIndex, true);
                }
                else
                {
                    dataGridMonthView.CurrentCell.Style.BackColor = Color.White;
                    SetIntervalValue(columnIndex, rowIndex, false);
                }
            }
        }

        private AdPlanModel GetSelectedPlan(int rowIndex)
        {
            if (dataGridMonthView.Rows[rowIndex].DataBoundItem != null)
            {
                return (AdPlanModel)dataGridMonthView.Rows[rowIndex].DataBoundItem;
            }

            return null;
        }
        private string GetMonth(int selectedMonth)
        {
            return Common.MonthsList.Where(i => i.MonthValue == selectedMonth).Select(i => i.MonthText).FirstOrDefault();
        }
        private void SetIntervalValue(int columnIndex, int rowIndex, bool value)
        {
            var cell = dataGridMonthView.Rows[rowIndex].Cells[columnIndex].OwningColumn.Name;
            var selectedPlan = GetSelectedPlan(rowIndex);

            switch (cell)
            {
                case "AM1":
                    rveBL.SetIntervalInAdPlan(selectedPlan.AdPlanId, value, IntervalEnum.AM1);
                    break;

                case "AM2":
                    rveBL.SetIntervalInAdPlan(selectedPlan.AdPlanId, value, IntervalEnum.AM2);
                    break;

                case "AM3":
                    rveBL.SetIntervalInAdPlan(selectedPlan.AdPlanId, value, IntervalEnum.AM3);
                    break;

                case "PM1":
                    rveBL.SetIntervalInAdPlan(selectedPlan.AdPlanId, value, IntervalEnum.PM1);
                    break;

                case "PM2":
                    rveBL.SetIntervalInAdPlan(selectedPlan.AdPlanId, value, IntervalEnum.PM2);
                    break;
            }
        }
    }


}
